package main

import (
	"fmt"
	"html/template"
	"net/http"
)

type Student struct {
	Name string
	Age  int
}

func sayHello(w http.ResponseWriter, r *http.Request) {
	// 解析指定文件生成模板对象
	tmpl, err := template.ParseFiles("./src/template/demo2/hello.tmpl")
	if err != nil {
		fmt.Println("创建模板异常, err:", err)
		return
	}
	//创建结构体
	var stu = Student{Name: "Alice", Age: 15}
	//创建map
	var user = map[string]string{
		"name": "李华",
		"age":  "34",
	}
	//定义切片
	var list = []string{"A", "B", "C", "D"}

	// 利用给定数据渲染模板，并将结果写入w
	tmpl.Execute(w, map[string]interface{}{
		"stu":  stu,
		"user": user,
		"list": list,
	})
}

func main() {
	http.HandleFunc("/", sayHello)
	err := http.ListenAndServe(":8088", nil)
	if err != nil {
		fmt.Println("HTTP服务器异常,err:", err)
		return
	}
}
